﻿namespace HZY.Repository.FreeSql.Admin.Repositories.Impl;

public class RepositoryImpl<T> : RepositoryBaseImpl<T>, IRepository<T> where T : class
{
    public RepositoryImpl(IFreeSql fsql, Expression<Func<T, bool>> filter = null, Func<string, string> asTable = null)
        : base(fsql, filter, asTable)
    {

    }

    /// <summary>
    /// 查询转换为分页视图模型 支持 Sql 字符串
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="page"></param>
    /// <param name="size"></param>
    /// <param name="orderBy"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    //public virtual async Task<PagingView> AsPagingViewAsync(string sql, int page, int size, string orderBy = "1", params object[] parameters)
    //{
    //    var pagingView = new PagingView(page, size);

    //    //如果分页码 小于 0 则代表导出 否则代表分页查询
    //    var sqlString = sql;
    //    if (page > 0)
    //    {
    //        pagingView.Total = await QuerySingleBySqlAsync<int>($"SELECT COUNT(1) FROM ({sql}) TAB", parameters);
    //        pagingView.PageCount = pagingView.Total % size > 0 ? (pagingView.Total / size) + 1 : (pagingView.Total / size);//无法整除有余,则增加一页
    //        pagingView.Page = pagingView.Page > pagingView.PageCount ? 1 : pagingView.Page;
    //        var offSet = size * (pagingView.Page - 1);
    //        sqlString = string.Empty;

    //        if (base.Context.Database.IsSqlServer())
    //        {
    //            sqlString = $"SELECT * FROM ({sql}) TAB ORDER BY {orderBy} OFFSET {offSet} ROWS FETCH NEXT {size} ROWS ONLY";
    //        }
    //        else if (base.Context.Database.IsMySql() || base.Context.Database.IsNpgsql())
    //        {
    //            sqlString = $"SELECT * FROM ({sql}) TAB ORDER BY {orderBy} LIMIT {size} OFFSET {offSet}";
    //        }
    //        else
    //        {
    //            throw new Exception("查询数据库类型不支持!");
    //        }
    //    }

    //    var data = await QueryDataTableBySqlAsync(sqlString, parameters);

    //    pagingView.BuilderDataSourceByDataTable(data, typeof(T));

    //    return pagingView;
    //}




}
